<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1" />
<title>MD5Test xref</title>
<link type="text/css" rel="stylesheet" href="../../../../../stylesheet.css" />
</head>
<body>
<pre>

<a name="1" href="#1">1</a>   <em class="jxr_comment">/*</em>
<a name="2" href="#2">2</a>   <em class="jxr_comment"> * To change this template, choose Tools | Templates</em>
<a name="3" href="#3">3</a>   <em class="jxr_comment"> * and open the template in the editor.</em>
<a name="4" href="#4">4</a>   <em class="jxr_comment"> */</em>
<a name="5" href="#5">5</a>   <strong class="jxr_keyword">package</strong> com.totsp.gwittir.client.util;
<a name="6" href="#6">6</a>   
<a name="7" href="#7">7</a>   <strong class="jxr_keyword">import</strong> junit.framework.TestCase;
<a name="8" href="#8">8</a>   
<a name="9" href="#9">9</a>   <strong class="jxr_keyword">import</strong> java.io.UnsupportedEncodingException;
<a name="10" href="#10">10</a>  
<a name="11" href="#11">11</a>  <strong class="jxr_keyword">import</strong> java.security.MessageDigest;
<a name="12" href="#12">12</a>  <strong class="jxr_keyword">import</strong> java.security.NoSuchAlgorithmException;
<a name="13" href="#13">13</a>  
<a name="14" href="#14">14</a>  
<a name="15" href="#15">15</a>  <em class="jxr_javadoccomment">/**</em>
<a name="16" href="#16">16</a>  <em class="jxr_javadoccomment"> *</em>
<a name="17" href="#17">17</a>  <em class="jxr_javadoccomment"> * @author kebernet</em>
<a name="18" href="#18">18</a>  <em class="jxr_javadoccomment"> */</em>
<a name="19" href="#19">19</a>  <strong class="jxr_keyword">public</strong> <strong class="jxr_keyword">class</strong> MD5Test <strong class="jxr_keyword">extends</strong> TestCase {
<a name="20" href="#20">20</a>      <strong class="jxr_keyword">public</strong> MD5Test(String testName) {
<a name="21" href="#21">21</a>          <strong class="jxr_keyword">super</strong>(testName);
<a name="22" href="#22">22</a>      }
<a name="23" href="#23">23</a>  
<a name="24" href="#24">24</a>      <strong class="jxr_keyword">public</strong> <strong class="jxr_keyword">static</strong> String MD5(String text)
<a name="25" href="#25">25</a>          <strong class="jxr_keyword">throws</strong> NoSuchAlgorithmException, UnsupportedEncodingException {
<a name="26" href="#26">26</a>          MessageDigest md;
<a name="27" href="#27">27</a>          md = MessageDigest.getInstance(<span class="jxr_string">"MD5"</span>);
<a name="28" href="#28">28</a>  
<a name="29" href="#29">29</a>          byte[] md5hash = <strong class="jxr_keyword">new</strong> byte[16];
<a name="30" href="#30">30</a>          md.update(text.getBytes(<span class="jxr_string">"UTF-8"</span>), 0, text.length());
<a name="31" href="#31">31</a>          md5hash = md.digest();
<a name="32" href="#32">32</a>  
<a name="33" href="#33">33</a>          <strong class="jxr_keyword">return</strong> toHexString(md5hash);
<a name="34" href="#34">34</a>      }
<a name="35" href="#35">35</a>  
<a name="36" href="#36">36</a>      <strong class="jxr_keyword">public</strong> <strong class="jxr_keyword">void</strong> testMD5() <strong class="jxr_keyword">throws</strong> Exception {
<a name="37" href="#37">37</a>          MD5 md5 = <strong class="jxr_keyword">new</strong> MD5(<span class="jxr_string">"This is a test."</span>);
<a name="38" href="#38">38</a>          String client = md5.asHex();
<a name="39" href="#39">39</a>          String server = MD5(<span class="jxr_string">"This is a test."</span>);
<a name="40" href="#40">40</a>          assertEquals(server, client);
<a name="41" href="#41">41</a>      }
<a name="42" href="#42">42</a>  
<a name="43" href="#43">43</a>      <strong class="jxr_keyword">private</strong> <strong class="jxr_keyword">static</strong> String toHexString(byte[] data) {
<a name="44" href="#44">44</a>          StringBuffer buf = <strong class="jxr_keyword">new</strong> StringBuffer();
<a name="45" href="#45">45</a>  
<a name="46" href="#46">46</a>          <strong class="jxr_keyword">for</strong> (<strong class="jxr_keyword">int</strong> i = 0; i &lt; data.length; i++) {
<a name="47" href="#47">47</a>              <strong class="jxr_keyword">int</strong> halfbyte = (data[i] &gt;&gt;&gt; 4) &amp; 0x0F;
<a name="48" href="#48">48</a>              <strong class="jxr_keyword">int</strong> two_halfs = 0;
<a name="49" href="#49">49</a>  
<a name="50" href="#50">50</a>              <strong class="jxr_keyword">do</strong> {
<a name="51" href="#51">51</a>                  <strong class="jxr_keyword">if</strong> ((0 &lt;= halfbyte) &amp;&amp; (halfbyte &lt;= 9)) {
<a name="52" href="#52">52</a>                      buf.append((<strong class="jxr_keyword">char</strong>) ('0' + halfbyte));
<a name="53" href="#53">53</a>                  } <strong class="jxr_keyword">else</strong> {
<a name="54" href="#54">54</a>                      buf.append((<strong class="jxr_keyword">char</strong>) ('a' + (halfbyte - 10)));
<a name="55" href="#55">55</a>                  }
<a name="56" href="#56">56</a>  
<a name="57" href="#57">57</a>                  halfbyte = data[i] &amp; 0x0F;
<a name="58" href="#58">58</a>              } <strong class="jxr_keyword">while</strong> (two_halfs++ &lt; 1);
<a name="59" href="#59">59</a>          }
<a name="60" href="#60">60</a>  
<a name="61" href="#61">61</a>          <strong class="jxr_keyword">return</strong> buf.toString();
<a name="62" href="#62">62</a>      }
<a name="63" href="#63">63</a>  }
</pre>
<hr/><div id="footer">This page was automatically generated by <a href="http://maven.apache.org/">Maven</a></div></body>
</html>

